#include "udpthread.h"

#include <QPixmap>

UdpThread::UdpThread(QUdpSocket *udpsocket)
{
    m_udpsocket = udpsocket;
}


void UdpThread::run()
{
    connect(m_udpsocket, &QUdpSocket::readyRead, this, &UdpThread::readPendingDatagrams);
}

void UdpThread::readPendingDatagrams()
{
    qDebug() << "有内容接收";

    while (m_udpsocket->hasPendingDatagrams())
    {
        QByteArray datagram;
        datagram.resize(m_udpsocket->pendingDatagramSize());

        m_udpsocket->readDatagram(datagram.data(), datagram.size(), &m_sender, &m_senderPort);
        qDebug() << "接收报文大小：" << datagram.size();

        QImage image;
        image.loadFromData(datagram);
        emit imgReturned(&image);

//        QSize newSize(520, 400); // 新的大小
//        image = image.scaled(newSize, Qt::KeepAspectRatio);
//        m_label->setPixmap(QPixmap::fromImage(image));

    }
}

void UdpThread::writeTo(QByteArray message)
{
    m_udpsocket->writeDatagram(message, m_sender, m_senderPort);
    qDebug() << "发送给IP：" << m_sender.toString() << m_senderPort << "端口";

}
